/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SessionBeans.Sharing;

import api.Rights.RightsEnum;
import entities.Rights;
import javax.annotation.security.PermitAll;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Tomas
 */
@Stateless
@PermitAll
@LocalBean
public class RightsService {
    @PersistenceContext(unitName = "tCloudEE-ejbPU")
    private EntityManager em;

    public Rights loadRights(RightsEnum rightsEnum) {
        Rights result;
        
        switch (rightsEnum) {
            case NEW:
                result = em.find(Rights.class, 4l);
                break;
            case READ:
                result = em.find(Rights.class, 1l);
                break;
            case WRITE:
                result = em.find(Rights.class, 2l);
                break;
            default:
                result = em.find(Rights.class, 3l);
                break;
          
        }
        return result;      
    }
    
    public RightsEnum loadEnum(Rights rights){
        switch (rights.getTypeCode()){
            case 1:
                return RightsEnum.READ;
            case 11:
                return RightsEnum.WRITE;
            case 111:
                return RightsEnum.NEW;
            case 1111:
                return RightsEnum.DELETE;
        }
        return RightsEnum.READ;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void persist(Object object) {
        em.persist(object);
    }
}
